﻿using Kent.Boogaart.HelperTrinity.Extensions;

namespace Kent.Boogaart.Gauntlet.RegenerationPolicies
{
	/// <summary>
	/// An implementation of <see cref="IRegenerationPolicy"/> that uses file timestamps to determine whether to regenerate a file.
	/// </summary>
	/// <remarks>
	/// <para>
	/// This class is a singleton, and the only instance can be accessed via the <see cref="Instance"/> property.
	/// </para>
	/// </remarks>
	public class TimestampRegenerationPolicy : IRegenerationPolicy
	{
		/// <summary>
		/// Gets the instance of <see cref="TimestampRegenerationPolicy"/>.
		/// </summary>
		public static readonly TimestampRegenerationPolicy Instance = new TimestampRegenerationPolicy();

		/// <summary>
		/// Determines whether to regenerate a file based on the timestamps of the input, output and template files.
		/// </summary>
		/// <param name="context">
		/// The context.
		/// </param>
		/// <returns>
		/// <see langword="false"/> if the output file is older than both the input file and the template file, otherwise
		/// <see langword="false"/>.
		/// </returns>
		public bool ShouldRegenerate(Context context)
		{
			context.AssertNotNull("context");

			if ((context.OutputFile.LastWriteTimeUtc > context.InputFile.LastWriteTimeUtc) && (context.OutputFile.LastWriteTimeUtc > context.TemplateFile.LastWriteTimeUtc))
			{
				return false;
			}

			return true;
		}
	}
}